// pages/approveInfo/approveInfo.js 
const app = getApp()
Page({

  /**
   * 页面的初始数据 
   */
  data: {
    logo: '',
    name: '',
    files: '',
    files2: '',
    info:'',
    isagree:false,
    choselogo:false
  },
  // 企业证件
  chooseImage: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files: res.tempFilePaths
        });
      }
    })
  },
  
  // 企业图片
  chooseImage2: function (e) {
    var that = this;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
      success(res) {
        // 返回选定照片的本地文件路径列表，tempFilePath可以作为img标签的src属性显示图片
        that.setData({
          files2: res.tempFilePaths
        });
      }
    })
  },
  /* ----------------------更改logo---------------------- */
  logoHandle: function () {
    let that = this
    let logo = that.data.logo
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'],
      success(res) {
        // tempFilePath可以作为img标签的src属性显示图片
        let tempFilePaths = res.tempFilePaths
        that.setData({
          choselogo:true,
          logo: tempFilePaths
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let that = this
    that.setData({
      avatar: app.siteinfo.userinfo.logo,
      nickName: app.siteinfo.userinfo.wechat_name,
    })
  },

  agreementHandle: function () {
    wx.navigateTo({
      url: '/pages/agreement/agreement',
    })
  },
  nameinput:function(e){
    this.setData({
      name:e.detail.value
    })
  },
  infoinput: function (e) {
    this.setData({
      info: e.detail.value
    })
  },
  agreehandle:function(){
    var that = this;
    that.setData({
      isagree:!that.data.isagree
    })
  },
  saveHandle:function(){
    var that = this;
    var files1 = that.data.files[0]
    var files2 = that.data.files2[0]
    var choselogo = that.data.choselogo
    if (choselogo){
      var logo = that.data.logo[0]
    }else{
      wx.showToast({
        title: '请上传企业logo',
        icon:'none'
      })
      return;
    }
    
    console.log(logo)
    var comname = that.data.name
    var info = that.data.info
    var isagree = that.data.isagree
    if (!files1){
      wx.showToast({
        title: '请上传营业执照',
        icon:"none"
      })
      return;
    } else if (!files2) {
      wx.showToast({
        title: '请上传企业图片',
        icon: "none"
      })
      return;
    } else if (!logo) {
      wx.showToast({
        title: '请上传企业logo',
        icon: "none"
      })
      return;
    } else if (!comname) {
      wx.showToast({
        title: '请填写企业名称',
        icon: "none"
      })
      return;
    } else if (!info) {
      wx.showToast({
        title: '请填写企业简介',
        icon: "none"
      })
      return;
    } else if (!isagree) {
      wx.showToast({
        title: '请同意企业认证协议',
        icon: "none"
      })
      return;
    }else{
      wx.uploadFile({
        url: app.siteinfo.rootpath + 'thumbupload',
        filePath: logo,
        name: 'file',
        header: {
          'content-type': 'multipart/form-data'
        },
        success: function (res) {
          if (res.data) {
            var logo = res.data
            wx.uploadFile({
              url: app.siteinfo.rootpath + 'thumbupload',
              filePath: files1,
              name: 'file',
              header: {
                'content-type': 'multipart/form-data'
              },
              success: function (res) {
                if (res.data) {
                  files1 = res.data
                  console.log(files1)
                  console.log(res.data)
                  wx.uploadFile({
                    url: app.siteinfo.rootpath + 'thumbupload',
                    filePath: files2,
                    name: 'file',
                    header: {
                      'content-type': 'multipart/form-data'
                    },
                    success: function (res) {
                      
                      if(res.data){
                        files2 = res.data
                        console.log(files2)
                        console.log(res.data)
                        wx.request({
                          url: app.siteinfo.rootpath+'comrz',
                          method:"post",
                          data:{
                             files1 : files1,
                             files2 : files2,
                             logo : logo,
                             comname: comname,
                             info: info,
                            uid: app.siteinfo.userinfo.id,
                            openid: app.siteinfo.userinfo.openid,
                          },
                          success:function(res){
                            if (res.data.code == 202) {
                              wx.showToast({
                                title: res.data.msg,
                                icon:"none"
                              })
                              return;
                            }
                            if(res.data.code == 200){
                              wx.request({
                                url: app.siteinfo.rootpath + 'paycomrz',
                                data: {
                                  id: res.data.id
                                },
                                method: 'post',
                                success: function (res) {
                                  console.log(res)
                                  wx.requestPayment({
                                    'appId': res.data.appId,
                                    'timeStamp': res.data.timeStamp,
                                    'nonceStr': res.data.nonceStr,
                                    'package': res.data.package,
                                    'signType': res.data.signType,
                                    'paySign': res.data.paySign,
                                    'success': function (res) {
                                      console.log(res);
                                      wx.showToast({
                                        title: '认证申请已提交'
                                      })
                                      setTimeout(function(){
                                        wx.switchTab({
                                          url: '/pages/mine/mine',
                                        })
                                      },1500)
                                    },
                                    'fail': function (res) {
                                      console.log(res);
                                    },
                                  })
                                }
                              })
                              
                            }else{
                              wx.showToast({
                                title: '认证申请失败',
                                icon: 'none'
                              })
                            }
                          }
                        })
                      }else{
                        wx.showToast({
                          title: '上传失败',
                          icon: 'none'
                        })
                      }
                    },
                    complete:function(){
                      wx.hideLoading()
                    }
                  })
                } else {
                  wx.showToast({
                    title: '上传失败',
                    icon: 'none'
                  })
                }
              }, fail: function () {
                wx.showToast({
                  title: '上传失败',
                  icon: 'none'
                })
              }
            })
          } else {
            wx.showToast({
              title: '上传失败',
              icon: 'none'
            })
          }
        }, fail: function () {
          wx.showToast({
            title: '上传失败',
            icon: 'none'
          })
        }
      })
    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})